package com.project.biz.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.biz.CmsOrderProductBakBiz;
import com.project.biz.CmsProductLabelBiz;
import com.project.biz.CmsProductSpecBiz;
import com.project.entity.CmsOrderProductBak;
import com.project.entity.CmsProduct;
import com.project.entity.CmsProductSku;
import com.project.entity.CmsProductSpec;
import com.project.mapper.CmsOrderProductBakMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 商品备份业务层接口实现类
 * @Author: zhuchi
 * @CreateDate: 2020/10/16 23:00
 * @Version: 1.0
 */
@Service
public class CmsOrderProductBakBizImpl extends ServiceImpl<CmsOrderProductBakMapper, CmsOrderProductBak> implements CmsOrderProductBakBiz {

    /**
     * 商品标签业务层接口类
     */
    @Autowired
    private CmsProductLabelBiz productLabelBiz;

    /**
     * 商品规格业务层接口类
     */
    @Autowired
    private CmsProductSpecBiz productSpecBiz;

    /**
     * 商品下单备份商品信息
     * @param product 商品信息
     * @param productSku 用户选择的商品SKU信息
     * @return 商品备份ID
     */
    public Long addOrderProductBak(CmsProduct product, CmsProductSku productSku){
        CmsOrderProductBak orderProductBak = new CmsOrderProductBak();
        BeanUtils.copyProperties(product, orderProductBak);

        //获取商品标签信息
        List<String> productLabels = this.productLabelBiz.getProductLabel(product.getProductId());

        //获取商品规格信息
        List<CmsProductSpec> productSpecs = this.productSpecBiz.getProductSpecs(product.getProductId());

        orderProductBak.setProductLabel(JSONObject.toJSONString(productLabels));
        orderProductBak.setProductSpecs(JSONObject.toJSONString(productSpecs));
        orderProductBak.setProductSkus(JSONObject.toJSONString(productSku));
        int result =  this.baseMapper.insert(orderProductBak);
        if (result > 0){
            return orderProductBak.getProductBakId();
        }

        return null;
    }

}
